using System;

using Microsoft.SPOT;
using Microsoft.SPOT.Input;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using Microsoft.SPOT.Hardware;

namespace eHouse
{
    public class LightsWindow: Window
    {

        private Window ligthswindow;

        Canvas canvas = new Canvas();
        SimpleButton button_kitchen_on;
        SimpleButton button_kitchen_off;
        SimpleButton button_living_on;
        SimpleButton button_living_off;
        SimpleButton button_bedroom_on;
        SimpleButton button_bedroom_off;
        SimpleButton button_bathroom_on;
        SimpleButton button_bathroom_off;
        SimpleButton button_switch_all_on;
        SimpleButton button_switch_all_off;

        public LightsWindow(Window mw)
        {
            ligthswindow = new Window();
            ligthswindow.Height = SystemMetrics.ScreenHeight;
            ligthswindow.Width = SystemMetrics.ScreenWidth;         
            
            CreateWindow();                                    
        }

        public void CreateWindow()
        {
            Text text_kitchen = new Text();
            text_kitchen.Font = Resources.GetFont(Resources.FontResources.NinaB);
            text_kitchen.TextContent = "Kitchen";
            text_kitchen.HorizontalAlignment = Microsoft.SPOT.Presentation.HorizontalAlignment.Center;
            text_kitchen.VerticalAlignment = Microsoft.SPOT.Presentation.VerticalAlignment.Center;
            Panel pan_kitchen = new Panel();
            pan_kitchen.Width = ligthswindow.Width / 2;
            pan_kitchen.Height = 4* ligthswindow.Height /25;
            pan_kitchen.Children.Add(text_kitchen);

            Text text_living = new Text();
            text_living.Font = Resources.GetFont(Resources.FontResources.NinaB);
            text_living.TextContent = "Living Room";
            text_living.HorizontalAlignment = Microsoft.SPOT.Presentation.HorizontalAlignment.Center;
            text_living.VerticalAlignment = Microsoft.SPOT.Presentation.VerticalAlignment.Center;
            Panel pan_living = new Panel();
            pan_living.Width = ligthswindow.Width / 2;
            pan_living.Height = 4 * ligthswindow.Height / 25;
            pan_living.Children.Add(text_living);

            Text text_bedroom = new Text();
            text_bedroom.Font = Resources.GetFont(Resources.FontResources.NinaB);
            text_bedroom.TextContent = "Bedroom";
            text_bedroom.HorizontalAlignment = Microsoft.SPOT.Presentation.HorizontalAlignment.Center;
            text_bedroom.VerticalAlignment = Microsoft.SPOT.Presentation.VerticalAlignment.Center;
            Panel pan_bedroom = new Panel();
            pan_bedroom.Width = ligthswindow.Width / 2;
            pan_bedroom.Height = 4 * ligthswindow.Height / 25;
            pan_bedroom.Children.Add(text_bedroom);

            Text text_bathroom = new Text();
            text_bathroom.Font = Resources.GetFont(Resources.FontResources.NinaB);
            text_bathroom.TextContent = "Bathroom";
            text_bathroom.HorizontalAlignment = Microsoft.SPOT.Presentation.HorizontalAlignment.Center;
            text_bathroom.VerticalAlignment = Microsoft.SPOT.Presentation.VerticalAlignment.Center;
            Panel pan_bathroom = new Panel();
            pan_bathroom.Width = ligthswindow.Width / 2;
            pan_bathroom.Height = 4 * ligthswindow.Height / 25;
            pan_bathroom.Children.Add(text_bathroom);

            Text text_all = new Text();
            text_all.Font = Resources.GetFont(Resources.FontResources.NinaB);
            text_all.TextContent = "All";
            text_all.HorizontalAlignment = Microsoft.SPOT.Presentation.HorizontalAlignment.Center;
            text_all.VerticalAlignment = Microsoft.SPOT.Presentation.VerticalAlignment.Center;
            Panel pan_all = new Panel();
            pan_all.Width = ligthswindow.Width / 2;
            pan_all.Height = 4 * ligthswindow.Height / 25;
            pan_all.Children.Add(text_all);

            Image image_kitchen_on = new Image(Resources.GetBitmap(Resources.BitmapResources.switch_on));
            image_kitchen_on.HorizontalAlignment = HorizontalAlignment.Center;
            image_kitchen_on.VerticalAlignment = VerticalAlignment.Center;
            button_kitchen_on = new SimpleButton(image_kitchen_on, ligthswindow.Width / 2, 4 * ligthswindow.Height / 25);
            button_kitchen_on.BackColor = Colors.White;

            Image image_kitchen_off = new Image(Resources.GetBitmap(Resources.BitmapResources.switch_off));
            image_kitchen_off.HorizontalAlignment = HorizontalAlignment.Center;
            image_kitchen_off.VerticalAlignment = VerticalAlignment.Center;
            button_kitchen_off = new SimpleButton(image_kitchen_off, ligthswindow.Width / 2, 4 * ligthswindow.Height / 25);
            button_kitchen_off.BackColor = Colors.White;

            Image image_living_on = new Image(Resources.GetBitmap(Resources.BitmapResources.switch_on));
            image_living_on.HorizontalAlignment = HorizontalAlignment.Center;
            image_living_on.VerticalAlignment = VerticalAlignment.Center;
            button_living_on = new SimpleButton(image_living_on, ligthswindow.Width / 2, 4 * ligthswindow.Height / 25);
            button_living_on.BackColor = Colors.White;

            Image image_living_off = new Image(Resources.GetBitmap(Resources.BitmapResources.switch_off));
            image_living_off.HorizontalAlignment = HorizontalAlignment.Center;
            image_living_off.VerticalAlignment = VerticalAlignment.Center;
            button_living_off = new SimpleButton(image_living_off, ligthswindow.Width / 2, 4 * ligthswindow.Height / 25);
            button_living_off.BackColor = Colors.White;

            Image image_bedroom_on = new Image(Resources.GetBitmap(Resources.BitmapResources.switch_on));
            image_bedroom_on.HorizontalAlignment = HorizontalAlignment.Center;
            image_bedroom_on.VerticalAlignment = VerticalAlignment.Center;
            button_bedroom_on = new SimpleButton(image_bedroom_on, ligthswindow.Width / 2, 4 * ligthswindow.Height / 25);
            button_bedroom_on.BackColor = Colors.White;

            Image image_bedroom_off = new Image(Resources.GetBitmap(Resources.BitmapResources.switch_off));
            image_bedroom_off.HorizontalAlignment = HorizontalAlignment.Center;
            image_bedroom_off.VerticalAlignment = VerticalAlignment.Center;
            button_bedroom_off = new SimpleButton(image_bedroom_off, ligthswindow.Width / 2, 4 * ligthswindow.Height / 25);
            button_bedroom_off.BackColor = Colors.White;

            Image image_bathroom_on = new Image(Resources.GetBitmap(Resources.BitmapResources.switch_on));
            image_bathroom_on.HorizontalAlignment = HorizontalAlignment.Center;
            image_bathroom_on.VerticalAlignment = VerticalAlignment.Center;
            button_bathroom_on = new SimpleButton(image_bathroom_on, ligthswindow.Width / 2, 4 * ligthswindow.Height / 25);
            button_bathroom_on.BackColor = Colors.White;

            Image image_bathroom_off = new Image(Resources.GetBitmap(Resources.BitmapResources.switch_off));
            image_bathroom_off.HorizontalAlignment = HorizontalAlignment.Center;
            image_bathroom_off.VerticalAlignment = VerticalAlignment.Center;
            button_bathroom_off = new SimpleButton(image_bathroom_off, ligthswindow.Width / 2, 4 * ligthswindow.Height / 25);
            button_bathroom_off.BackColor = Colors.White;

            Text text_switch_all_on = new Text(Resources.GetFont(Resources.FontResources.NinaB), "On");
            text_switch_all_on.HorizontalAlignment = HorizontalAlignment.Center;
            text_switch_all_on.VerticalAlignment = VerticalAlignment.Center;
            button_switch_all_on = new SimpleButton(text_switch_all_on, ligthswindow.Width /4, 4 * ligthswindow.Height / 25);
            button_switch_all_on.BackColor = Colors.White;

            Text text_switch_all_off = new Text(Resources.GetFont(Resources.FontResources.NinaB), "Off");
            text_switch_all_off.HorizontalAlignment = HorizontalAlignment.Center;
            text_switch_all_off.VerticalAlignment = VerticalAlignment.Center;
            button_switch_all_off = new SimpleButton(text_switch_all_off, ligthswindow.Width / 4, 4 * ligthswindow.Height / 25);
            button_switch_all_off.BackColor = Colors.White;

            bool[] lightsState = LightsBL.GetInstance().GetLightsState();

            Canvas.SetTop(pan_kitchen, 0);
            Canvas.SetLeft(pan_kitchen, 0);
            canvas.Children.Add(pan_kitchen);
            if(lightsState[0])
            {
                Canvas.SetTop(button_kitchen_on, 0);
                Canvas.SetRight(button_kitchen_on, 0);
                canvas.Children.Add(button_kitchen_on);
            }
                else
            {
                Canvas.SetTop(button_kitchen_off, 0);
                Canvas.SetRight(button_kitchen_off, 0);
                canvas.Children.Add(button_kitchen_off);
            }

            Canvas.SetTop(pan_living, 4 * ligthswindow.Height / 25);
            Canvas.SetLeft(pan_living, 0);
            canvas.Children.Add(pan_living);
            if (lightsState[1])
            {
                Canvas.SetTop(button_living_on, 4 * ligthswindow.Height / 25);
                Canvas.SetRight(button_living_on, 0);
                canvas.Children.Add(button_living_on);
            }
            else
            {
                Canvas.SetTop(button_living_off, 4 * ligthswindow.Height / 25);
                Canvas.SetRight(button_living_off, 0);
                canvas.Children.Add(button_living_off);
            }

            Canvas.SetTop(pan_bedroom, 8 * ligthswindow.Height / 25);
            Canvas.SetLeft(pan_bedroom, 0);
            canvas.Children.Add(pan_bedroom);
            if (lightsState[2])
            {
                Canvas.SetTop(button_bedroom_on, 8 * ligthswindow.Height / 25);
                Canvas.SetRight(button_bedroom_on, 0);
                canvas.Children.Add(button_bedroom_on);
            }
            else
            {
                Canvas.SetTop(button_bedroom_off, 8 * ligthswindow.Height / 25);
                Canvas.SetRight(button_bedroom_off, 0);
                canvas.Children.Add(button_bedroom_off);               
            }

            Canvas.SetTop(pan_bathroom,12* ligthswindow.Height / 25);
            Canvas.SetLeft(pan_bathroom, 0);
            canvas.Children.Add(pan_bathroom);
            if (lightsState[3])
            {
                Canvas.SetTop(button_bathroom_on, 12 * ligthswindow.Height / 25);
                Canvas.SetRight(button_bathroom_on, 0);
                canvas.Children.Add(button_bathroom_on);
            }
            else
            {
                Canvas.SetTop(button_bathroom_off, 12 * ligthswindow.Height / 25);
                Canvas.SetRight(button_bathroom_off, 0);
                canvas.Children.Add(button_bathroom_off);                
            }

            Canvas.SetTop(pan_all, 16 * ligthswindow.Height / 25);
            Canvas.SetLeft(pan_all, 0);
            canvas.Children.Add(pan_all);

            Canvas.SetTop(button_switch_all_on, 16 * ligthswindow.Height / 25);
            Canvas.SetLeft(button_switch_all_on, ligthswindow.Width/2);
            canvas.Children.Add(button_switch_all_on);

            Canvas.SetTop(button_switch_all_off, 16 * ligthswindow.Height / 25);
            Canvas.SetRight(button_switch_all_off, 0);
            canvas.Children.Add(button_switch_all_off);

            Image image_schedule = new Image(Resources.GetBitmap(Resources.BitmapResources.schedule));
            image_schedule.HorizontalAlignment = HorizontalAlignment.Center;
            image_schedule.VerticalAlignment = VerticalAlignment.Center;
            SimpleButton button_schedule = new SimpleButton(image_schedule, ligthswindow.Width / 2, ligthswindow.Height / 5);
            button_schedule.BackColor = Colors.White;
            Canvas.SetBottom(button_schedule, 0);
            Canvas.SetLeft(button_schedule, 0);
            canvas.Children.Add(button_schedule);

            Image image_return = new Image(Resources.GetBitmap(Resources.BitmapResources.imreturn));
            image_return.HorizontalAlignment = HorizontalAlignment.Center;
            image_return.VerticalAlignment = VerticalAlignment.Center;
            SimpleButton button_return = new SimpleButton(image_return,ligthswindow.Width/2, ligthswindow.Height/5);
            button_return.BackColor = Colors.White;
            Canvas.SetBottom(button_return, 0);
            Canvas.SetRight(button_return, 0);
            canvas.Children.Add(button_return);

            //Button actions

            button_kitchen_on.Click += new EventHandler(buttonKitchenOn_Click);
            button_kitchen_off.Click += new EventHandler(buttonKitchenOff_Click);
            button_living_on.Click += new EventHandler(buttonLivingOn_Click);
            button_living_off.Click += new EventHandler(buttonLivingOff_Click);
            button_bedroom_on.Click += new EventHandler(buttonBedroomOn_Click);
            button_bedroom_off.Click += new EventHandler(buttonBedroomOff_Click);
            button_bathroom_on.Click += new EventHandler(buttonBathroomOn_Click);
            button_bathroom_off.Click += new EventHandler(buttonBathroomOff_Click);
            button_switch_all_off.Click += new EventHandler(buttonAllOff_Click);
            button_switch_all_on.Click += new EventHandler(buttonAllOn_Click);
            button_schedule.Click += new EventHandler(buttonSchedule_Click);
            button_return.Click += new EventHandler(buttonReturn_Click);

                ligthswindow.Child = canvas;

                ligthswindow.Visibility = Visibility.Visible;

            
        }

        #region Click Handlers
        void buttonKitchenOn_Click(object sender, EventArgs evt)
        {
            LightsBL.GetInstance().SetLight("kitchen", false);
            Debug.Print("Switching off kitchen lights");         
            canvas.Children.Remove(button_kitchen_on);

            Canvas.SetTop(button_kitchen_off, 0);
            Canvas.SetRight(button_kitchen_off, 0);
            canvas.Children.Add(button_kitchen_off);   
        }

        void buttonKitchenOff_Click(object sender, EventArgs evt)
        {
            LightsBL.GetInstance().SetLight("kitchen", true);
            Debug.Print("Switching on kitchen lights");            
            canvas.Children.Remove(button_kitchen_off);

            Canvas.SetTop(button_kitchen_on, 0);
            Canvas.SetRight(button_kitchen_on, 0);
            canvas.Children.Add(button_kitchen_on);
        }

        void buttonLivingOn_Click(object sender, EventArgs evt)
        {
            LightsBL.GetInstance().SetLight("living", false);
            Debug.Print("Switching off living room lights");
            canvas.Children.Remove(button_living_on);

            Canvas.SetTop(button_living_off, 4 * ligthswindow.Height / 25);
            Canvas.SetRight(button_living_off, 0);
            canvas.Children.Add(button_living_off);
        }

        void buttonLivingOff_Click(object sender, EventArgs evt)
        {
            LightsBL.GetInstance().SetLight("living", true);
            Debug.Print("Switching on living room lights");
            canvas.Children.Remove(button_living_off);

            Canvas.SetTop(button_living_on, 4 * ligthswindow.Height / 25);
            Canvas.SetRight(button_living_on, 0);
            canvas.Children.Add(button_living_on);
        }

        void buttonBedroomOn_Click(object sender, EventArgs evt)
        {
            LightsBL.GetInstance().SetLight("bedroom", false);
            Debug.Print("Switching off bedroom lights");
            canvas.Children.Remove(button_bedroom_on);

            Canvas.SetTop(button_bedroom_off, 8 * ligthswindow.Height / 25);
            Canvas.SetRight(button_bedroom_off, 0);
            canvas.Children.Add(button_bedroom_off);
        }

        void buttonBedroomOff_Click(object sender, EventArgs evt)
        {
            LightsBL.GetInstance().SetLight("bedroom", true);
            Debug.Print("Switching on bedroom lights");
            canvas.Children.Remove(button_bedroom_off);

            Canvas.SetTop(button_bedroom_on, 8 * ligthswindow.Height / 25);
            Canvas.SetRight(button_bedroom_on, 0);
            canvas.Children.Add(button_bedroom_on);
        }
        

        void buttonBathroomOn_Click(object sender, EventArgs evt)
        {
            LightsBL.GetInstance().SetLight("bathroom", false);
            Debug.Print("Switching off bathroom lights");
            canvas.Children.Remove(button_bathroom_on);

            Canvas.SetTop(button_bathroom_off, 12 * ligthswindow.Height / 25);
            Canvas.SetRight(button_bathroom_off, 0);
            canvas.Children.Add(button_bathroom_off);
        }

        void buttonBathroomOff_Click(object sender, EventArgs evt)
        {
            LightsBL.GetInstance().SetLight("bathroom", true);
            Debug.Print("Switching on bathroom lights");
            canvas.Children.Remove(button_bathroom_off);

            Canvas.SetTop(button_bathroom_on, 12 * ligthswindow.Height / 25);
            Canvas.SetRight(button_bathroom_on, 0);
            canvas.Children.Add(button_bathroom_on);
        }

        void buttonAllOff_Click(object sender, EventArgs evt)
        {
            LightsBL.GetInstance().SetAllLigths(false);

            if (canvas.Children.Contains(button_kitchen_on))
            {
                canvas.Children.Remove(button_kitchen_on);
                Canvas.SetTop(button_kitchen_off, 0);
                Canvas.SetRight(button_kitchen_off, 0);
                canvas.Children.Add(button_kitchen_off);
            }

            if (canvas.Children.Contains(button_living_on))
            {
                canvas.Children.Remove(button_living_on);
                Canvas.SetTop(button_living_off, 4 * ligthswindow.Height / 25);
                Canvas.SetRight(button_living_off, 0);
                canvas.Children.Add(button_living_off);
            }

            if (canvas.Children.Contains(button_bedroom_on))
            {
                canvas.Children.Remove(button_bedroom_on);
                Canvas.SetTop(button_bedroom_off, 8 * ligthswindow.Height / 25);
                Canvas.SetRight(button_bedroom_off, 0);
                canvas.Children.Add(button_bedroom_off);
            }

            if (canvas.Children.Contains(button_bathroom_on))
            {
                canvas.Children.Remove(button_bathroom_on);
                Canvas.SetTop(button_bathroom_off, 12 * ligthswindow.Height / 25);
                Canvas.SetRight(button_bathroom_off, 0);
                canvas.Children.Add(button_bathroom_off);
            }
            

            
        }

        void buttonAllOn_Click(object sender, EventArgs evt)
        {
            LightsBL.GetInstance().SetAllLigths(true);

            if (canvas.Children.Contains(button_kitchen_off))
            {
                canvas.Children.Remove(button_kitchen_off);
                Canvas.SetTop(button_kitchen_on, 0);
                Canvas.SetRight(button_kitchen_on, 0);
                canvas.Children.Add(button_kitchen_on);
            }

            if (canvas.Children.Contains(button_living_off))
            {
                canvas.Children.Remove(button_living_off);
                Canvas.SetTop(button_living_on, 4 * ligthswindow.Height / 25);
                Canvas.SetRight(button_living_on, 0);
                canvas.Children.Add(button_living_on);
            }

            if (canvas.Children.Contains(button_bedroom_off))
            {
                canvas.Children.Remove(button_bedroom_off);
                Canvas.SetTop(button_bedroom_on, 8 * ligthswindow.Height / 25);
                Canvas.SetRight(button_bedroom_on, 0);
                canvas.Children.Add(button_bedroom_on);
            }

            if (canvas.Children.Contains(button_bathroom_off))
            {
                canvas.Children.Remove(button_bathroom_off);
                Canvas.SetTop(button_bathroom_on, 12 * ligthswindow.Height / 25);
                Canvas.SetRight(button_bathroom_on, 0);
                canvas.Children.Add(button_bathroom_on);
            }
                       

            
        }

        void buttonSchedule_Click(object sender, EventArgs evt)
        {
            LightsScheduleWindow lsw = new LightsScheduleWindow(this);
        }
        void buttonReturn_Click(object sender, EventArgs evt)
        {
            ligthswindow.Close();
        }
#endregion
    }
}
